package feng.process.load;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;

import feng.process.cont.ProcessConst;

/**
 * 流程配置加载器：从conf.properties中process.dir的路径下查找precess.collection.added所配置的流程
 * @author 冯昌硕
 * @create 2012-10-22 下午9:48:42
 */
public class ProcessConfigLoader {
    
    private static final Logger log = Logger.getLogger(ProcessConfigLoader.class);
    
    private Properties properties;
    
    /**
     * 记录conf.properties配置项中的各流程模板文件名
     */
    private List<String> processTempleteLst = new ArrayList<String>();
    
    public ProcessConfigLoader() {
        properties = new Properties();
    }
    
    /**
     * 功能：初始化操作，加载conf.properties配置文件
     * @create 2012-10-22 下午9:54:10
     */
    public boolean init() {
        log.debug("开始加载配置文件：" + ProcessConst.CONF_FILE);
        InputStream cfgFileIs = null;
        try {
            
            // 取conf.properties配置文件目录
            String userDir = System.getProperty("user.dir");
            log.debug("user.dir"+ userDir +"]");
            String path = userDir + File.separator + ProcessConst.CONF_DIR + File.separator + ProcessConst.CONF_FILE;
            log.debug("path=["+ path +"]");
            
            cfgFileIs = new BufferedInputStream(new FileInputStream(path));
            properties.load(cfgFileIs);
            
            selectProcessFiles();
            
            return true;
        } catch (IOException e) {
            log.debug("加载配置文件" + ProcessConst.CONF_FILE + "异常：" + e.toString());
            e.printStackTrace();
            return false;
        } finally {
            if(cfgFileIs != null) {
                try {
                    cfgFileIs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } 
            }
        }
    }
    
    /**
     * 功能：从conf.properties配置项中取出各流程模板配置文件的文件名
     * @create 2012-10-25 上午12:03:10
     */
    private void selectProcessFiles() {
        log.debug(ProcessConst.CONF_FILE + "文件配置项：");
        Set<Entry<Object, Object>> propSet = properties.entrySet();
        Iterator<Entry<Object, Object>> propIt = propSet.iterator();
        
        Entry<Object, Object> propEntry = null;
        Object key = null;
        Object value = null;
        while(propIt.hasNext()) {
            propEntry = propIt.next();
            key = propEntry.getKey();
            value = propEntry.getValue();
            log.debug("key=["+ key +"] value=["+value+"]");
            
            if(String.valueOf(key).startsWith(ProcessConst.KEY_PROCESS_LOADED_DOT)) {
                processTempleteLst.add(String.valueOf(value));
            }
        }
    }
    
    /**
     * 功能：从Properties中取指定Key的值
     * @param key 键
     * @return 值
     * @create 2012-10-22 下午10:21:13
     */
    public String getValue(String key) {
        String value = properties.getProperty(key);
        return value;
    }

    public List<String> getProcessTempleteLst() {
        return processTempleteLst;
    }
    
}
